package cn.mengfly.testnrz.im.client.handler;

import cn.mengfly.testnrz.im.ProtoMsg;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 聊天处理器
 *
 * @author Mengfly
 */
@ChannelHandler.Sharable
public class ChatMsgHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        if (!(msg instanceof ProtoMsg.Message)) {
            super.channelRead(ctx, msg);
            return;
        }
        ProtoMsg.Message pkg = ((ProtoMsg.Message) msg);
        ProtoMsg.HeadType type = pkg.getType();
        if (!type.equals(ProtoMsg.HeadType.MESSAGE_REQUEST)) {
            super.channelRead(ctx, msg);
            return;
        }

        ProtoMsg.MessageRequest messageRequest = pkg.getMessageRequest();
        String content = messageRequest.getContent();
        String uId = messageRequest.getFrom();
        System.out.println(" 收到消息 from uid:" + uId + "->" + content);
    }
}
